const express = require('express');
const JWT = require('jsonwebtoken');
const expressJWT = require('express-jwt')
const app = express();
const secretKey = 'app_express_secretKey';
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(expressJWT({ secret: secretKey, algorithms: ['HS256'] }).unless({ path: [/^\/api\//] }))

app.post('/api1/post', (req, res) => {
  let userInfoBody = req.body
  let JWTStr = JWT.sign({ username: userInfoBody }, secretKey, { expiresIn: '3000s' })
  res.send({
    status: 1,
    msg: '登录成功',
    token: JWTStr
  })
});

app.get('/api1/get', (req, res) => {
  let adata = req.user
  res.send({
    status: 1,
    msg: '请求成功',
    data:adata
  })
});

app.listen(3000, () => {
  console.log('success')
});